package top.infopub.mgr.mkt.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.mkt.MarketingRewardDto;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.MarketingRewardDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.dao.ProjectInfoDao;
import top.infopub.mgr.mkt.domain.MarketingRewardDomain;
import top.infopub.mgr.mkt.jsonvo.MarketingRewardJsonVo;
import top.infopub.mgr.mkt.service.MarketingRewardService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.service.SetupProService;
import top.infopub.mgr.mkt.vo.MarketingRewardVo;
import top.infopub.model.mkt.MarketingReward;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.model.mkt.ProjectInfo;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.Date;
import java.util.List;

@Service
public class MarketingRewardServiceImpl extends GenericServiceImpl<MarketingReward, String> implements MarketingRewardService {

    @Autowired
    private MarketingRewardDao marketingRewardDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private SetupProService setupProService;

    @Autowired
    private ProjectInfoDao projectInfoDao;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Override
    public GenericDao<MarketingReward, String> getDao() {
        return marketingRewardDao;
    }

    @Override
    public Page<MarketingRewardDomain> selectPageByDomainWhere(Page<MarketingRewardDomain> page,
                                                               MarketingRewardVo marketingRewardVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<MarketingRewardDomain>) marketingRewardDao.selectByDomainWhere(marketingRewardVo);
    }

    @Override
    public MarketingRewardDomain queryById(String id) {
        return marketingRewardDao.queryById(id);
    }

    @Override
    public MarketingRewardJsonVo queryDetailById(String id) {
        MarketingRewardJsonVo mktRwdJson = new MarketingRewardJsonVo();
        //基础信息查询
        MarketingRewardDto mktRwdDto = marketingRewardDao.queryDetailById(id);
        mktRwdJson.setInfo(mktRwdDto);

        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(mktRwdDto.getProCode(), ProjectFileTypeEnum.FILE_TYPE40.getCode(), null);
        mktRwdJson.setFileAtt(setupProService.fileAttaListToArray(projectAttaList));
        return mktRwdJson;
    }

    @Override
    public RspVo editDetail(MarketingReward mktRwd, List<ProjectAttachment> attachmentList, AuthorizationVO vo, Integer submitType) {
        //根据pro_code查询项目基础信息
        ProjectInfo tmpProInfo = projectInfoDao.findByProCode(mktRwd.getProCode());
        if (tmpProInfo == null) return RspVo.getFailureResponse(400, "项目基础信息不存在");
        mktRwd.setUptBy(vo.getId());
        mktRwd.setUptTime(new Date());
        if (StringUtils.isNotBlank(mktRwd.getId())) {
            MarketingReward tmpMR = marketingRewardDao.selectByPrimaryKey(mktRwd.getId());
            if (tmpMR == null) return RspVo.getFailureResponse(400, "营销奖励申请不存在");
            //判断审批状态
            if (StringUtils.isNotBlank(tmpMR.getProcessStatus())
                    && (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpMR.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), tmpMR.getProcessStatus()))) {
                return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
            }

            marketingRewardDao.modifyByIdSelective(mktRwd);
        } else {
            mktRwd.setAddBy(vo.getId());
            mktRwd.setAddTime(new Date());
            mktRwd.setCodes(IdFactory.getUUIDUpper());
            marketingRewardDao.insertByProcess(mktRwd);
            marketingRewardDao.modifyByIdSelective(mktRwd);
        }

        //更新附件信息
        projectAttachmentDao.deleteByWhere(mktRwd.getProCode(), ProjectFileTypeEnum.FILE_TYPE40.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }

        if (submitType == 1) {
            String detailJson = JSON.toJSONString(queryDetailById(mktRwd.getId()));
            proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE19, detailJson, mktRwd.getId(), mktRwd.getProCode(), vo);
        }
        return RspVo.getSuccessResponse();
    }
}
